package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.GlobalInfo;
import com.moleq.models.PosTender;
import com.moleq.models.RegKey;

public class RegKeyService {
	private DatabaseHelper DBHelper;
	private Context mContext;
	private String errorString;

	public RegKeyService() {

	}

	public RegKeyService(Context context) {
		// this.DBHelper = new DatabaseHelper(context);
		mContext = context;
		this.DBHelper = new DatabaseHelper(
				GlobalInfo.getInstance().ShareContext);
	}

	public List<RegKey> FindAll() {
		List<RegKey> obj = new ArrayList<RegKey>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_reg_key", null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			String rk_code = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.RegKey.RK_CODE));
			String rk_key = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.RegKey.RK_KEY));

			obj.add(new RegKey(rk_code,rk_key));
		}
		cursor.close();
		return obj;
	}

	public RegKey Find(int no) {

		RegKey obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from t_reg_key where rk_code  = " + no, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			String rk_code = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.RegKey.RK_CODE));
			String rk_key = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.RegKey.RK_KEY));
			
			obj = new RegKey(rk_code,rk_key);

		}
		cursor.close();
		return obj;

	}

	public void Insert(RegKey obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"INSERT INTO " + DatabaseSchema.RegKey.SCHEMA_NAME + " VALUES(?,?)",
				new Object[] { obj.getRk_code(), obj.getRk_key()});
	}

	
	public void Update(RegKey obj){
	SQLiteDatabase db = DBHelper.getWritableDatabase();
	db.execSQL(
			"Update " + DatabaseSchema.RegKey.SCHEMA_NAME + " set rk_code = ?,rk_key = ?, where rk_key = ?",
			new Object[] { obj.getRk_code(), obj.getRk_key(),obj.getRk_code()});
	}

	
}
